<style>
.pure_form_extra .mandatory { color: #ff0000; padding-left: 5px; }
.pure_table_extra { width: 350px; margin-top: 5px; }
.pure_table_extra th { height: 25px; line-height: 25px; }
.pure_table_extra td { vertical-align: middle; }
.pure_table_extra select { width: 100%; }
.pure_table_extra2 { table-layout: fixed; }
.pure_table_extra2 th { white-space: nowrap; height: 25px; line-height: 25px; }
.pure_table_extra2 td { white-space: nowrap; vertical-align: middle; }
.pure-button-add { color: #fff !important; background: rgb(223, 117, 20) !important; }
</style>

<?php 
$date = date('Y-m-d', time());

$id = Utils::get_param('id', -1);
$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $id);
$classes = $orm->get('pb_colg_class', NULL, NULL, NULL, NULL, $opts)->d;

$pg = Utils::get_param('pg', 1);
$opts = new stdClass();
$opts->cd = 't.type = :v1';
$opts->cdv = array(':v1' => PT_LANGUAGE);
$objs = $orm->get('pb_colg_class', 't.name', 'ASC', $pg, SIZE, $opts);

$opts = new stdClass();
$opts->cd = 't.class_id = :v1';
$opts->cdv = array(':v1' => $id);
$data = $orm->get('pb_colg_rsltlog', 't.date', 'DESC', NULL, NULL, $opts)->d;
?>


<div class="pure-form">
  <button onclick="get_rslt(0, $('#rslt_date').val());" class="pure-button pure-button-primary">New Test</button>&nbsp;
  <input id="rslt_date" type="text" value="<?php echo $date; ?>" />
</div>

<table class="pure-table pure_table_extra" cellpadding=0 cellspacing=0>
  <thead>
    <tr>
      <th class="ui-widget-header">Date</th>
      <th class="ui-widget-header" style="width: 40px;">&nbsp;</th>
    </tr>
  </thead>
  <tbody id="data_body"></tbody>
</table>

<div id="dialog_change" class="pure-form" style="display: none;" title="&nbsp;">
  <table id="rslt_data" class="pure-table pure_table_extra2" cellpadding=0 cellspacing=0></table>
</div>

<script type="text/javascript">
  /*<![CDATA[*/
  var __date = '<?php echo $date; ?>';
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __objs = JSON.parse('<?php echo addslashes(json_encode($objs)); ?>');
  var __classes = JSON.parse('<?php echo addslashes(json_encode($classes)); ?>');
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __rslt;
  
  $(function() {
    $('#rslt_date').datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: "yy-mm-dd",
      showButtonPanel: true
    });
    reset();
    render();
  });
  
  function reset() {
    __data = __data.sort(function(a, b) {
      return Utils.to_date(a.date).getTime() < Utils.to_date(b.date).getTime();
    });
  };

  function render() {
    Utils.leftbar(
      __objs,
      __classes[0].id,
      'Language Class',
      'name',
      function(page) {
        Url.redirect(__redmd, __sys + '.php?i=' + __id + '&__file=' + __file + '&id=' + __classes[0].id + '&pg=' + page);
      }
    );
    render_data();
  };
  
  function render_data() {
    Url.insert_param('id', __classes[0].id);
    $('#data_body').empty();
    for (var idx in __data) {
      var itm = __data[idx];
      $('#data_body').append(
        '<tr class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.date + '</td>' +
          '<td>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-pencil" onclick="get_rslt(' + itm.id + ', \'' + itm.date + '\');"></span>' +
            '<span style="cursor: pointer; float: left;" class="ui-button-icon-primary ui-icon ui-icon-closethick" onclick="delete_rslt(' + itm.id + ');"></span>' +
          '</td>' + 
        '</tr>'
      );
    }
    if (__data.length == 0) {
      $('#data_body').append('<tr class="pure-table-even"><td colspan=6>None</td></tr>');
    }
  };

  function delete_rslt(id) {
    var req = {
      id: id
    };
    
    Ajax.request(
      __requrl + 'delete_rslt',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        Utils.splice_by_id(id, __data);
        render_data();
      },
      { confirm: 1 }
    );
  };

  function get_rslt(id, date) {
    var req = {
      id: id,
      date: date,
      class_id: __classes[0].id
    };
    
    Ajax.request(
      __requrl + 'get_rslt',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __rslt = rslt;
        open_rslt(id, date);
      },
      null
    );
  };

  function open_rslt(id, date) {
    var func = function() {
      Ajax.request(
        __requrl + 'rslt',
        'req=' + Utils.encodeURI(JSON.stringify(__rslt)),
        function(rslt) {
          $('#dialog_change').dialog('close');
          render_data();
        },
        null
      );
    };
    
    $('#dialog_change').dialog({
      width: 600,
      buttons: {
        'OK': function() {
          func();
        },
        'Cancel': function() { 
          $(this).dialog('close');
        }
      }
    });
    $('#dialog_change').dialog('option', 'title', date);
    
    set_rslt();
  };

  function set_rslt() {
    $('#rslt_data').empty();
    $('#rslt_data').append('<tr id="rslt_header"><th style="width: 60px;">Student ID</th></tr>');
    for (var idx in __rslt.logs[0].header__) {
      var itm = __rslt.logs[0].header__[idx];
      $('#rslt_header').append(
        '<th style="width: 100px;">' + itm.name + '</th>'
      );
    }

    $('#rslt_data').append('<tbody id="rslt_body"></tbody>');
    for (var idx in __rslt.students) {
      var itm = __rslt.students[idx];
      $('#rslt_body').append(
        '<tr id="rslt_rw_' + idx + '" class="' + ((idx % 2 == 0) ? 'pure-table-even' : 'pure-table-odd') + '">' + 
          '<td>' + itm.__id + '</td>' +
        '</tr>'
      );

      for (var idx2 in __rslt.logs[0].header__) {
        var itm2 = __rslt.logs[0].header__[idx2];

        var sc = -2;
        for (var idx3 in __rslt.rslts) {
          itm3 = __rslt.rslts[idx3];
          if (itm3.hd_track_id == itm2.track_id && itm3.student_id == itm.student_id) {
            sc = itm3.score; 
          }
        }

        if (sc == -2) {
          sc = 0;
          var obj = {
            student_id: itm.student_id,
            class_id: itm.class_id,
            hd_track_id: itm2.track_id,
            score: sc,
            resit__: 0
          };
          __rslt.rslts.push(obj);
        }
        
        $('#rslt_rw_' + idx).append(
          '<td>' +
            '<input onchange="update_rslt(' + itm.student_id + ', \'' + itm2.track_id + '\', this.value, 0)" style="width: 90px;" type="text" value="' + ((sc == -1) ? 'INC' : sc) + '" />%' +
          '</td>'
        );
      }
    }
  };

  function update_rslt(student_id, hd_track_id, value, resit) {
    for (var idx in __rslt.rslts) {
      var itm = __rslt.rslts[idx];
      if (itm.student_id == student_id && itm.hd_track_id == hd_track_id && itm.resit__ == resit) {
        itm.score = (value.toLowerCase() == ('INC').toLowerCase()) ? - 1 : value;
        itm.__c = 1;
      }
    }
    set_rslt();
  };

  function get_obj(id) {
    var req = {
      id: id
    };
    Ajax.request(
      __requrl + 'get_obj',
      'req=' + Utils.encodeURI(JSON.stringify(req)),
      function(rslt) {
        __data = rslt.__data;
        delete rslt.__data;
        __classes = rslt;
        render();
      },
      null
    );
    return false;
  };
  /*]]>*/
</script>